package com.sneakxy.cloudbase.platform.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.session.web.http.CookieHttpSessionIdResolver;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.HttpSessionIdResolver;

import com.sneakxy.cloudbase.platform.http.CloudBaseCookieSerializer;
import com.sneakxy.cloudbase.platform.http.CookieValueHandler;
import com.sneakxy.cloudbase.platform.http.TokenCookieValueHandler;
import com.sneakxy.cloudbase.platform.utils.feign.TokenHolder;

@Configuration
public class SpringSessionConfig {

	@Bean
	public HttpSessionIdResolver webSessionIdResolver(CookieSerializer cookieSerializer) {
		CookieHttpSessionIdResolver sessionIdResolver = new CookieHttpSessionIdResolver();
		sessionIdResolver.setCookieSerializer(cookieSerializer);
		return sessionIdResolver;
	}
	
	@Lazy
	@Bean
	public CookieSerializer cookieSerializer() {
		CloudBaseCookieSerializer cookieSerializer = new CloudBaseCookieSerializer();
		cookieSerializer.setCookiePath("/");
		cookieSerializer.setCookieName(TokenHolder.SESSIOIN_COOKIE_NAME);
		return cookieSerializer;
	}
	
	@Bean
	public CookieValueHandler tokenCookieValueHandler() {
		return new TokenCookieValueHandler();
	}
	
}
